//
//  KNConfig.m
//  KNBaseModule
//
//  Created by vbn on 2019/1/22.
//  Copyright © 2019 pori. All rights reserved.
//

#import "KNConfig.h"


@interface KNConfig ()

@property (strong, nonatomic) NSDictionary *instances;

@property (strong, nonatomic) NSObject *jsonObject;

@end

@implementation KNConfig

+ (instancetype)shareInstance {
    static KNConfig *instance;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = [[KNConfig alloc] init];
    });
    return instance;
}

+ (instancetype)configWithPath:(NSString *)filePath; {
    KNConfig *config = [KNConfig shareInstance].instances[filePath];
    if (config == nil) {
        config = [[KNConfig alloc] initWithFilePath:filePath];
        [[KNConfig shareInstance].instances setValue:config forKey:filePath];
    }
    return config;
}

+ (instancetype)configWithName:(NSString *)fileName {
    return [self configWithName:fileName type:@"json"];
}

+ (instancetype)configWithName:(NSString *)fileName type:(NSString *)type {
    NSString *filePath = [[NSBundle mainBundle] pathForResource:fileName ofType:type];
    return [self configWithPath:filePath];
}

- (instancetype)initWithFilePath:(NSString *)filePath {
    self = [super init];
    if (self) {
        NSData *data = [NSData dataWithContentsOfFile:filePath];
        self.jsonObject = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
    }
    return self;
}

- (id)valueForKeyPath:(NSString *)keyPath {
    return [self.jsonObject valueForKeyPath:keyPath];
}

- (id)valueForKey:(NSString *)key {
    return [self.jsonObject valueForKey:key];
}

@end





